/**
 * 分页小插件
 *
 * by yihy
 *
 * 使用说明
 *
 * $("#123").PageNav({
 *           baseUrl: "chi_{page}.html",
 *           nowPage: 10,
 *           countNum: 1000
 *       });
 *
 *
 *
 *   baseUrl  基础分页的url，也可以是JavaScript 方法
 *   previousNoClick   上一页不可点击的标签的Class
 *   previousClick     上一页可以点击的标签的Class
 *   nextNoClick       下一页不可点击的标签的Class
 *   nextClick         下一页可以点击的标签的Class
 *   now                当前页的标签的Class
 *   general            普通页标签的Class
 *
 *   nowPage            当前页码
 *   countNum           总页码
 *
 * {baseUrl} 表示基础baseUrl
 * {pageNumber} 表示页码
 * {page}  url中需要被替换成页码的标识
 *
 *
 *
 *
 previousNoClick: "prev disabled",
 previousClick: "prev",
 nextNoClick: "next disabled",
 nextClick: "next",
 now: "active",
 general: "",
 nowPage: 1,
 countNum: 1
 *
 *
 */

// previousNoClick: "<li class='prev disabled'><a href='javascript:void(0);'>«</a></li>",
//     previousClick: "<li class='prev'><a href='{baseUrl}'>«</a></li>",
//     nextNoClick: "<li class='next disabled'><a href='javascript:void(0);'>»</a></li>",
//     nextClick: "<li class='next'><a href='{baseUrl}'>»</a></li>",
//     now: "<li class='active'><a href='{baseUrl}'>{pageNumber}</a></li>",
//     general: "<li><a href='{baseUrl}'>{pageNumber}</a></li>",


(function ($) {

    $.fn.extend({

        //将可选择的变量传递给方法
        "PageNav": function (options) {


            //设置默认值并用逗号隔开
            var defaults = {
                baseUrl: "",
                previousNoClick: "disabled",
                previousClick: "",
                nextNoClick: "disabled",
                nextClick: "",
                now: "active",
                general: "",
                nowPage: 1,
                countNum: 1
            }

            var options = $.extend(defaults, options);

            return this.each(function () {

                return new PageNav(this, options);
                // alert("----------");
            });
        }
    });


})(jQuery);


function PageNav(e, options) {


  var  previousNoClick="<li ><a href='javascript:void(0);'>«</a></li>";
    var  previousClick= "<li><a href='{baseUrl}'>«</a></li>";
    var  nextNoClick= "<li><a href='javascript:void(0);'>»</a></li>";
    var  nextClick= "<li><a href='{baseUrl}'>»</a></li>";
    var now= "<li><a href='{baseUrl}'>{pageNumber}</a></li>";
    var general= "<li><a href='{baseUrl}'>{pageNumber}</a></li>";


    options.previousNoClick=$(previousNoClick).addClass(options.previousNoClick).prop("outerHTML");
    options.previousClick=$(previousClick).addClass(options.previousClick).prop("outerHTML");
    options.nextNoClick=$(nextNoClick).addClass(options.nextNoClick).prop("outerHTML");
    options.nextClick=$(nextClick).addClass(options.nextClick).prop("outerHTML");
    options.now=$(now).addClass(options.now).prop("outerHTML");
    options.general=$(general).addClass(options.general).prop("outerHTML");




    options.previousClick = options.previousClick.replace(/{baseUrl}/, options.baseUrl);
    options.nextClick = options.nextClick.replace(/{baseUrl}/, options.baseUrl);
    options.now = options.now.replace(/{baseUrl}/, options.baseUrl);
    options.general = options.general.replace(/{baseUrl}/, options.baseUrl);


    var o = options;

    var a = makepage();

    $(e).html(a);

    //alert(a);
    // init : function(){
    //
    //     makepage();
    // };


    /**
     *  分页函数   页码可以传 String、int类型等
     * @param snowPage   当前页
     * @param sumPages   总页数
     * @param baseurl    url (url中需要替换成页数的位置写成{page})
     * @returns {String}
     */
    function makepage() {


        var init = o;

        var nowPage = parseInt(init.nowPage);
        var sumPage = parseInt(init.countNum);

        var a = paging(nowPage, sumPage);


        var result = "";

        if (a[0] > 0) {

            result += init.previousClick.replace(/{page}/, a[0])+"\n";


        } else {
            result += init.previousNoClick;
        }

        for (var int = 1; int < a.length - 1; int++) {

            if (a[int] == nowPage) {
                result += init.now.replace(/{page}/, a[int]).replace(/{pageNumber}/, a[int])+"\n";
            } else {
                result += init.general.replace(/{page}/, a[int]).replace(/{pageNumber}/, a[int])+"\n";
            }

        }

        if (a[a.length - 1] > 0 && nowPage + 1 <= sumPage) {


            result += init.nextClick.replace(/{page}/, a[a.length - 1])+"\n";
        } else {
            result += init.nextNoClick;
        }

        return result += "";
    }


    function paging(nowPage, sumPage) {
        /**
         * 总页数小于2页
         */
        if (sumPage < 2) {
            //alert("总页数<=2页");
            var a = new Array();
            a[0] = 0;
            a[1] = 1;
            a[2] = 0;

            return a;


            /**
             * 总页数大于5页
             */
        } else if (sumPage > 5) {
            //alert("总页数>=5页");
            var a = new Array();
            a[0] = nowPage - 1;
            if (sumPage / 2 > nowPage) {
//			nowPage=1;
                //nowPage-1=1;

                if (nowPage == 1) {
                    a[1] = nowPage;
                    a[2] = nowPage + 1;
                    a[3] = nowPage + 2;
                    a[4] = nowPage + 3;
                    a[5] = sumPage;
                } else if (nowPage == 2) {
                    a[1] = 1;
                    a[2] = nowPage;
                    a[3] = nowPage + 1;
                    a[4] = nowPage + 2;
                    a[5] = sumPage;
                } else {
                    a[1] = 1;
                    a[2] = nowPage - 1;
                    a[3] = nowPage;
                    a[4] = nowPage + 1;
                    a[5] = sumPage;
                }

            } else {
                if (nowPage == sumPage) {

                    a[1] = 1;
                    a[2] = nowPage - 3;
                    a[3] = nowPage - 2;
                    a[4] = nowPage - 1;
                    a[5] = sumPage;


                } else if (nowPage + 1 == sumPage) {

                    a[1] = 1;
                    a[2] = nowPage - 2;
                    a[3] = nowPage - 1;
                    a[4] = nowPage;
                    a[5] = sumPage;
                } else {
                    a[1] = 1;
                    a[2] = nowPage - 1;
                    a[3] = nowPage;
                    a[4] = nowPage + 1;
                    a[5] = sumPage;
                }


            }
            //alert("当前页"+nowPage);
            a[6] = nowPage + 1;
            if (sumPage == nowPage)
                a[6] = 0;
            return a;

            /**
             * 2页<=总页数<=5页
             */
        } else {

            //alert("2页<=总页数<=5页");
            var a = new Array();
            a[0] = nowPage - 1;
            for (var int = 1; int < sumPage + 1; int++) {
                a[int] = int;
            }


            a[sumPage + 1] = nowPage + 1;
            if (sumPage == nowPage)
                a[sumPage + 1] = 0;

            return a;
        }
    }


}

